<?php

namespace App\Model\Auth;


use App\Model\Member\MemberModel;
use App\Model\Referrer\ReferrerModel;

/**
 * 角色管理
 * Class RoleModel
 * @package App\Model\Auth
 */
class RoleModel extends \App\Core\BaseModel{

    protected $tableName = 'ss_role';

    const ROLE_INSIDE     = 1; //平台账号
    const ROLE_BUSINESS   = 2; //推广员账号
    const ROLE_WAREHOUSE  = 3; //仓库账号

    const TYPE_INSIDE = 1;//平台账号
    const TYPE_BUSINESS = 2;//推广员账号
    const TYPE_WAREHOUSE = 3;//仓库账号

    protected $casts = [
        'name'      => 'string',
        'state'     => 'integer',
        'is_action' => 'integer',
    ];

    public static function getTypeDict(?string $key = null){
        return self::getMap([
            self::TYPE_INSIDE    => '平台账号',
//            self::TYPE_BUSINESS  => '推广员账号',
//            self::TYPE_WAREHOUSE => '仓库账号',
        ], $key);
    }

    /**
     * 字典
     * @return array
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function dictList($merchants_id = 0,$type = 0){
        $where = [
            'state'     => self::ENABLED,
        ];

        if ( !empty($merchants_id) ){
            $where['merchants_id'] = $merchants_id;
        }
        if ($type > 0) {
            $where['type'] = $type;
        }
        $where['id'] = [MemberModel::ADMIN_ROLE_ID, '!='];
        return self::makeQueryModel($where)->field(['id', 'name', 'type'])->all()->toArray();
    }


}
